$(function(){
	if($('.menu .cur').length){
		var index = $('.menu .item').index($('.menu .cur'));
		var width = $('.menu .line').width();
		lineMove(index * width);
	}else{
		lineMove(0);
	}

	//主菜单
	$('.menu .item').hover(function(){
		var index = $('.menu .item').index($(this));
		var width = $('.menu .line').width();
		var num = $(this).find('ul li').size();
		lineMove(index * width);
		if(num > 0){
			$(this).find('ul').addClass('show');
		}
	}, function(){
		var index = $('.menu .item').index($('.menu .cur')) || 0;
		var width = $('.menu .line').width();
		lineMove(index * width);
		$(this).find('ul').removeClass('show');
	});

	function lineMove(move){
		$('.menu .line').css({'left': move + 'px'});
	}



	//获取地址栏hash参数
	function getHashParameter(key){
		var arr = (location.hash || "").replace(/^\#/,'').split("&");
		return arr[0];
	}
	var hashv = getHashParameter();
	if(hashv){
		var move = $('#' + hashv).offset().top;
		console.log(hashv);
		var $item = $('.submenu-list').find('a[data-id="'+ hashv +'"]').parent();
		$item.addClass('cur');
		var index = $('.submenu-list .submenu-item').index($item);
		var height = $('.submenu-list .submenu-item').height();
		arrowMove(index * height + index + 1);
		scrollTo(move);
	}else{
		//二级菜单
		if($('.submenu-list .cur').length){
			var index = $('.submenu-list .submenu-item').index($('.submenu-list .cur'));
			var height = $('.submenu-list .submenu-item').height();
			arrowMove(index * height + index + 1);
		}else{
			arrowMove(1);
		}
	}
	



	$('.submenu-list .submenu-item').hover(function(){
		var index = $('.submenu-list .submenu-item').index($(this));
		var height = $(this).height();
		arrowMove(index * height + index + 1);
	}, function(){
		var index = $('.submenu-list .submenu-item').index($('.submenu-list .cur'));
		index = index > 0 ? index : 0;
		var height = $(this).height();
		arrowMove(index * height + index + 1);
	});

	$('.submenu-list .submenu-item  a').click(function(){
		var type = $(this).attr('data-type');
		var id = $(this).attr('data-id');
		if(type == 'scroll'){
			var move = $('#' + id).offset().top;
			scrollTo(move);
			$('.submenu-list .submenu-item').removeClass('cur');
			$(this).parent().addClass('cur');
		}
	})

	function scrollTo(move){
		$('html, body').animate({scrollTop: move}, 600);
	}

	function arrowMove(move){
		$('.submenu-list .submenu-arrow').css({'top': move + 'px'});
	}

	//菜单跟随
	$(window).scroll(function (event) {
		var top = $(window).scrollTop();
		var height = $('.header').height();
		if(top > height){
			//console.log($(window).scrollTop());
			$('body').addClass('fix-header');
		}else{
			$('body').removeClass('fix-header');
		}
	})

	//首页轮播图
	if($('.swiper-container').length){
		var swiper = new Swiper('.swiper-container', {
			autoplay: {
				delay: 4000,
				stopOnLastSlide: false,
				disableOnInteraction: true,
			},
			effect : 'fade',
			loop : true,
			pagination: {
				el: '.swiper-pagination',
				clickable: true			
			}
		});
	}
  
	//手机版菜单展开
	$(document).on('click', '.mobile-logo .close', function(){
		$('.menu').removeClass('mobile-menu');
		$('html').removeClass('body-overflow');
	})
	$(document).on('click', '.mobile-open', function(){
		$('.menu').addClass('mobile-menu');
		$('html').addClass('body-overflow');
	})
	
});